using System.Runtime.InteropServices;
using Timer = System.Windows.Forms.Timer;

public partial class AutoCloseMessageBox : Form
{
    private readonly Timer _closeTimer;

    [DllImport("user32.dll", SetLastError = true)]
    private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);

    [DllImport("user32.dll", SetLastError = true)]
    private static extern int GetWindowLong(IntPtr hWnd, int nIndex);

    private const int GWL_EXSTYLE = -20;
    private const int WS_EX_LAYERED = 0x80000;
    private const int WS_EX_TRANSPARENT = 0x20;

    public AutoCloseMessageBox(string message)
    {
        InitializeComponent();
        lblMessage.Text = message;

        // 设置窗口样式
        this.TopMost = true; // 窗口总是最上层
        this.FormBorderStyle = FormBorderStyle.None; // 无边框
        this.StartPosition = FormStartPosition.CenterScreen; // 屏幕中央显示
        this.ShowInTaskbar = false; // 不显示在任务栏
        this.Opacity = 0.8; // 设置透明度

        // 设置鼠标穿透
        this.Load += (s, e) =>
        {
            int exStyle = GetWindowLong(this.Handle, GWL_EXSTYLE);
            SetWindowLong(this.Handle, GWL_EXSTYLE, exStyle | WS_EX_LAYERED | WS_EX_TRANSPARENT);
        };

        // 初始化计时器
        _closeTimer = new Timer { Interval = 500 }; // 3秒后关闭
        _closeTimer.Tick += (s, e) => Close();
        _closeTimer.Start();

        // 鼠标点击立即关闭
        this.MouseDown += (s, e) => Close();
    }

    #region Windows Form Designer generated code
    private Label lblMessage;

    private void InitializeComponent()
    {
        this.lblMessage = new System.Windows.Forms.Label();
        this.SuspendLayout();
        // 
        // lblMessage
        // 
        this.lblMessage.Dock = System.Windows.Forms.DockStyle.Fill;
        this.lblMessage.Font = new System.Drawing.Font("微软雅黑", 12F);
        this.lblMessage.Location = new System.Drawing.Point(10, 10);
        this.lblMessage.Name = "lblMessage";
        this.lblMessage.Size = new System.Drawing.Size(280, 80);
        this.lblMessage.TabIndex = 0;
        this.lblMessage.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
        // 
        // AutoCloseMessageBox
        // 
        this.ClientSize = new System.Drawing.Size(300, 100);
        this.Controls.Add(this.lblMessage);
        this.Padding = new System.Windows.Forms.Padding(10);
        this.Name = "AutoCloseMessageBox";
        this.ResumeLayout(false);
    }
    #endregion
}
